home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / MSG Shell ƒ / msg graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  11.7 KB  |  522 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg graphics.c
  4.  
  5. Purpose:    This module handles the about box, about MSG box, main
  6.             window opening/closing/updating, and help windows
  7.             opening/closing/updating.
  8.  
  9.  
  10. Ghost -=- a classic word-building challenge
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg graphics.h"
  31. #include "msg dialogs.h"
  32. #include "msg error.h"
  33. #include "msg menus.h"
  34. #include "msg sounds.h"
  35. #include "msg environment.h"
  36. #include "msg prefs.h"
  37. #include "ghost globals.h"
  38. #include "ghost graphics.h"
  39. #include "ghost help.h"
  40.  
  41. Boolean            gInitedWindowBounds;
  42. Rect            gMainScreenBounds;
  43. Rect            gMainWindowBounds;
  44. GDHandle        gBiggestDevice;
  45. WindowPtr        gMainWindow;
  46. WindowPtr        gHelp[NUM_HELP];
  47. int                gHelpWidth;
  48. int                gHelpHeight;
  49. int                gWindowWidth;
  50. int                gWindowHeight;
  51.  
  52. /* internal stuff */
  53. Rect            bRect;
  54. Ptr                myBits;
  55. CGrafPort        myCGrafPort;
  56. CGrafPtr        myCGrafPtr;
  57. CTabHandle        ourCMHandle;
  58. GrafPort        myGrafPort;
  59. GrafPtr            myGrafPtr;
  60.  
  61. void InitMSGGraphics(void)
  62. {
  63.     int                i;
  64.     
  65.     gHelpHeight=200;
  66.     gHelpWidth=300;
  67.     
  68.     gWindowWidth=400;
  69.     gWindowHeight=250;
  70.     
  71.     myCGrafPtr=0L;
  72.     myGrafPtr=0L;
  73.     
  74.     for (i=0; i<NUM_HELP; i++)
  75.         gHelp[i]=0L;
  76.     
  77.     gMainWindow=0L;
  78. }
  79.  
  80. void ShowInformation(void)
  81. {
  82.     DialogPtr        theDlog;
  83.     int                itemSelected = 0;
  84.     int                newleft;
  85.     int                newtop;
  86.     int                dlogID;
  87.  
  88.     if(GetWindowDepth() > 2)
  89.         dlogID = colorInfoDialog;
  90.     else
  91.         dlogID = bwInfoDialog;
  92.     
  93.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  94.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  95.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  96.                 theDlog->portRect.left)) / 2);
  97.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  98.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  99.                 theDlog->portRect.top)) / 2);
  100.     if(newtop < 15)
  101.         newtop = 15;
  102.     MoveWindow(theDlog, newleft, newtop, TRUE);
  103.     ShowWindow(theDlog);
  104.     
  105.     while(itemSelected == 0)
  106.     {
  107.         ModalDialog(0L, &itemSelected);
  108.     }
  109.     HideWindow(theDlog);
  110.     DisposeDialog(theDlog);
  111. }
  112.  
  113. void ShowSplashScreen(void)
  114. {
  115.     int                dlogID;
  116.     DialogPtr        theDlog;
  117.     int                itemSelected = 0;
  118.     int                newleft;
  119.     int                newtop;
  120.     unsigned int    keys[8];
  121.     int                itemType;
  122.     Handle            itemH;
  123.     Rect            box;
  124.     unsigned char    temp[7];
  125.     
  126.     GetKeys(&keys);
  127.     
  128.     if(GetWindowDepth() > 2)
  129.         dlogID = colorSplashDialog;
  130.     else
  131.         dlogID = bwSplashDialog;
  132.     
  133.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  134.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  135.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  136.                 theDlog->portRect.left)) / 2);
  137.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  138.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  139.                 theDlog->portRect.top)) / 2);
  140.     if(newtop < 15)
  141.         newtop = 15;
  142.     MoveWindow(theDlog, newleft, newtop, TRUE);
  143.     
  144.     if (keys[3]&4)
  145.     {
  146.         GetDItem(theDlog, 3, &itemType, &itemH, &box);
  147.         SetIText((ControlHandle)itemH, "\p");
  148.         ParamText("\pForgive your enemies, but", "\pnever forget their names. -JFK","\p","\p");
  149.     }
  150.     else
  151.         ParamText(gMyName,gMyOrg,"\p","\p");
  152.  
  153.     DoSound(sound_aboutMSG, TRUE);
  154.     OpenTheSoundDevice();
  155.     ShowWindow(theDlog);
  156.     while(itemSelected == 0)
  157.         ModalDialog(ProcOFilter, &itemSelected);
  158.     
  159.     HideWindow(theDlog);
  160.     DisposeDialog(theDlog);
  161.     CloseTheSoundDevice();
  162. }
  163.  
  164. void OpenMainWindow(void)
  165. {
  166.     if(!gMainWindow)
  167.     {
  168.         if(!gInitedWindowBounds)
  169.         {
  170.             gMainWindowBounds.left = gMainScreenBounds.left + (((gMainScreenBounds.right -
  171.                         gMainScreenBounds.left) - gWindowWidth) / 2);
  172.             gMainWindowBounds.top = 9+ gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  173.                         gMainScreenBounds.top) - gWindowHeight) / 2);
  174.             if(gMainWindowBounds.top < 30)
  175.                 gMainWindowBounds.top = 30;
  176.             gMainWindowBounds.bottom = gMainWindowBounds.top + gWindowHeight;
  177.             gMainWindowBounds.right = gMainWindowBounds.left + gWindowWidth;
  178.             gInitedWindowBounds = 1;
  179.         }
  180.         
  181.         if(gHasColorQD)
  182.         {
  183.             gMainWindow = NewCWindow(0L, &gMainWindowBounds, "\pGhost", TRUE,
  184.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  185.         }
  186.         else
  187.         {
  188.             gMainWindow = NewWindow(0L, &gMainWindowBounds, "\pGhost", TRUE,
  189.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  190.         }
  191.  
  192.         bRect = gMainWindow->portRect;
  193.     }
  194.     
  195.     if(gMainWindow)
  196.     {
  197.         SetPort(gMainWindow);
  198.         InvalRect(&(gMainWindow->portRect));
  199.     }
  200.     else ErrorString("\pThere is not enough memory to open the main window.","\p");
  201. }
  202.  
  203. void GetMainScreenBounds(void)
  204. {
  205.     gMainScreenBounds = screenBits.bounds;
  206.     gMainScreenBounds.top += MBarHeight;
  207. }
  208.  
  209. int GetWindowDepth(void)
  210. {
  211.     Rect        tempRect;
  212.     long        biggestSize;
  213.     long        tempSize;
  214.     GDHandle    thisHandle;
  215.     
  216.     if(gHasColorQD)
  217.     {
  218.         if(gMainWindow)
  219.         {
  220.             thisHandle = GetDeviceList();
  221.             gBiggestDevice = 0L;
  222.             biggestSize = 0L;
  223.             
  224.             while(thisHandle)
  225.             {
  226.                 if(TestDeviceAttribute(thisHandle, screenDevice) &&
  227.                             TestDeviceAttribute(thisHandle, screenActive))
  228.                     if(SectRect(&(gMainWindow->portRect), &((**thisHandle).gdRect),
  229.                                 &tempRect))
  230.                         if(biggestSize < (tempSize =
  231.                                 ((long)(tempRect.bottom - tempRect.top))
  232.                                 * ((long)(tempRect.right - tempRect.left))))
  233.                         {
  234.                             biggestSize = tempSize;
  235.                             gBiggestDevice = thisHandle;
  236.                         }
  237.                 thisHandle = GetNextDevice(thisHandle);
  238.             }
  239.             
  240.             if(gBiggestDevice)
  241.                 return (**(**gBiggestDevice).gdPMap).pixelSize;
  242.             else
  243.                 return 1;
  244.         }
  245.         else
  246.         {
  247.             return (**(**GetMainDevice()).gdPMap).pixelSize;
  248.         }
  249.     }
  250.     else
  251.     {
  252.         return 1;
  253.     }
  254. }
  255.  
  256. void UpdateBoard(void)
  257. {
  258.     long        offRowBytes, sizeOfOff;
  259.     int            theDepth, i, err;
  260.     GDHandle    oldDevice;
  261.     static int    gLastDepth;
  262.  
  263.     if((theDepth = GetWindowDepth()) > 2)
  264.     {
  265.         /* if we just changed from one color depth to another color depth */
  266.         if((myCGrafPtr != 0) && (gLastDepth != theDepth))
  267.         {
  268.             DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  269.             DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  270.             CloseCPort(myCGrafPtr);
  271.             myCGrafPtr = 0;
  272.         }
  273.         
  274.         if (myCGrafPtr==0L)
  275.         {
  276.             /* if we just switched from b/w to color, delete the b/w port */
  277.             if(myGrafPtr != 0L)
  278.             {
  279.                 DisposePtr(myGrafPort.portBits.baseAddr);
  280.                 ClosePort(myGrafPtr);
  281.                 myGrafPtr = 0;
  282.             }
  283.             
  284.             if(gBiggestDevice)
  285.             {
  286.                 oldDevice = GetGDevice();
  287.                 SetGDevice(gBiggestDevice);
  288.             }
  289.             else
  290.                 oldDevice = 0;
  291.             
  292.             myCGrafPtr = &myCGrafPort;
  293.             OpenCPort(myCGrafPtr);
  294.             gLastDepth = theDepth = (**(myCGrafPort).portPixMap).pixelSize;
  295.             
  296.             offRowBytes = (((theDepth * (bRect.right - bRect.left)) + 15) >> 4) << 1;
  297.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  298.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  299.             
  300.             myBits = NewPtr(sizeOfOff);
  301.             if(myBits == 0L)
  302.             {
  303.                 CloseCPort(myCGrafPtr);
  304.                 myCGrafPtr=0L;
  305.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  306.             }
  307.             
  308.             (**(myCGrafPort).portPixMap).baseAddr = myBits;
  309.             (**(myCGrafPort).portPixMap).rowBytes = offRowBytes + 0x8000;
  310.             (**(myCGrafPort).portPixMap).bounds = bRect;
  311.             
  312.             myCGrafPort.portRect = bRect;
  313.             
  314.             ourCMHandle = (**(**gBiggestDevice).gdPMap).pmTable;
  315.             err = HandToHand(&ourCMHandle);
  316.             if(err != noErr)
  317.             {
  318.                 DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  319.                 CloseCPort(myCGrafPtr);
  320.                 myCGrafPtr=0L;
  321.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  322.             }
  323.             
  324.             for(i = 0; i <= (**ourCMHandle).ctSize; i++)
  325.                 (**ourCMHandle).ctTable[i].value = i;
  326.             (**ourCMHandle).ctFlags &= 0x7fff;
  327.             (**ourCMHandle).ctSeed = GetCTSeed();
  328.             
  329.             (**(myCGrafPort).portPixMap).pmTable = ourCMHandle;
  330.             
  331.             if(oldDevice)
  332.                 SetGDevice(oldDevice);
  333.         }
  334.         
  335.         UpdateBoardColor();
  336.     }
  337.     else
  338.     {
  339.         if (myGrafPtr==0L)
  340.         {
  341.             if(myCGrafPtr != 0)
  342.             {
  343.                 DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  344.                 DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  345.                 CloseCPort(myCGrafPtr);
  346.                 myCGrafPtr = 0;
  347.             }
  348.             
  349.             myGrafPtr = &myGrafPort;
  350.             OpenPort(myGrafPtr);
  351.             
  352.             offRowBytes = (((bRect.right - bRect.left) + 15) >> 4) << 1;
  353.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  354.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  355.             
  356.             myBits = NewPtr(sizeOfOff);
  357.             if(myBits == 0L)
  358.             {
  359.                 ClosePort(myGrafPtr);
  360.                 myGrafPtr=0L;
  361.                 ErrorString("\pThere is not enough memory to open the main window.", "\p");
  362.             }
  363.             
  364.             myGrafPort.portBits.baseAddr = myBits;
  365.             myGrafPort.portBits.rowBytes = offRowBytes;
  366.             myGrafPort.portBits.bounds = bRect;
  367.             myGrafPort.portRect = bRect;
  368.         }
  369.         
  370.         UpdateBoardBW();
  371.     }
  372. }
  373.  
  374. void UpdateBoardColor(void)
  375. {
  376.     GDHandle    oldDevice;
  377.     RgnHandle    oldClipRgn;
  378.     
  379.     SetPort(gMainWindow);
  380.     
  381.     oldDevice = GetGDevice();
  382.     SetGDevice(gBiggestDevice);
  383.     
  384.     oldClipRgn = myCGrafPort.clipRgn;
  385.     myCGrafPort.clipRgn = gMainWindow->visRgn;
  386.     
  387.     SetPort((GrafPtr)myCGrafPtr);
  388.     
  389.     DrawBoardColor();
  390.     
  391.     SetPort(gMainWindow);
  392.     SetGDevice(oldDevice);
  393.     
  394.     CopyBits(&(((GrafPtr)myCGrafPtr)->portBits),
  395.                 &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  396.     
  397.     myCGrafPort.clipRgn = oldClipRgn;
  398. }
  399.  
  400. void UpdateBoardBW(void)
  401. {
  402.     RgnHandle    oldClipRgn;
  403.     
  404.     SetPort(gMainWindow);
  405.     
  406.     oldClipRgn = myGrafPort.clipRgn;
  407.     myGrafPort.clipRgn = gMainWindow->visRgn;
  408.     
  409.     SetPort(myGrafPtr);
  410.     
  411.     DrawBoardBW();
  412.     
  413.     SetPort(gMainWindow);
  414.     
  415.     CopyBits(&(myGrafPtr->portBits),
  416.                 &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  417.     
  418.     myGrafPort.clipRgn = oldClipRgn;
  419. }
  420.  
  421. void CloseMainWindow(void)
  422. {
  423.     DisposeWindow(gMainWindow);
  424.     gMainWindow=0L;
  425.     
  426.     gInProgress=FALSE;
  427.     AdjustMenus();
  428. }
  429.  
  430. void ShutDownMSGGraphics(void)
  431. {
  432.     if ((myCGrafPtr!=0L) || (myGrafPtr!=0L))
  433.         DisposPtr(myBits);
  434.     if(myCGrafPtr != 0)
  435.     {
  436.         DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  437.         CloseCPort(myCGrafPtr);
  438.         myCGrafPtr = 0;
  439.     }
  440.     if(myGrafPtr != 0L)
  441.     {
  442.         ClosePort(myGrafPtr);
  443.         myGrafPtr = 0;
  444.     }
  445. }
  446.  
  447. void OpenHelpWindow(int whichHelp)
  448. {
  449.     Rect    helpRect;
  450.     
  451.     if (!(gHelp[whichHelp]))
  452.     {
  453.         helpRect.left=10+20*whichHelp;
  454.         helpRect.top=50+20*whichHelp;
  455.         helpRect.bottom=helpRect.top+gHelpHeight;
  456.         helpRect.right=helpRect.left+gHelpWidth;
  457.         
  458.         gHelp[whichHelp]=NewWindow(0L, &helpRect, "\p", TRUE, noGrowDocProc, 
  459.                                     (WindowPtr)-1L, TRUE, 0L);
  460.         SetHelpTitle(whichHelp);
  461.     }
  462.     
  463.     SetPort(gHelp[whichHelp]);
  464.     InvalRect(&((gHelp[whichHelp])->portRect));
  465. }
  466.  
  467. void SetHelpTitle(int whichHelp)
  468. {
  469.     Str255        name;
  470.     
  471.     GetItem(gHelpMenu, whichHelp + 1, name);
  472.     SetWTitle(gHelp[whichHelp], name);
  473. }
  474.  
  475. void UpdateHelp(int whichHelp)
  476. {
  477.     long        offRowBytes, sizeOfOff;
  478.     Ptr            myBits;
  479.     Rect        bRect;
  480.     GrafPort    myGrafPort;
  481.     GrafPtr        myGrafPtr;
  482.     RgnHandle    oldClipRgn;
  483.     
  484.     SetPort(gHelp[whichHelp]);
  485.     
  486.     bRect = (gHelp[whichHelp])->portRect;
  487.     
  488.     myGrafPtr = &myGrafPort;
  489.     OpenPort(myGrafPtr);
  490.     
  491.     offRowBytes = ((gHelpWidth + 15) >> 4) << 1;
  492.     sizeOfOff = (long)(gHelpHeight) * offRowBytes;
  493.     OffsetRect(&bRect, -bRect.left, -bRect.top);
  494.     
  495.     myBits = NewPtr(sizeOfOff);
  496.     if(myBits == 0L)
  497.         ErrorString("\pThere is not enough memory.  ", "\p");
  498.     
  499.     myGrafPort.portBits.baseAddr = myBits;
  500.     myGrafPort.portBits.rowBytes = offRowBytes;
  501.     myGrafPort.portBits.bounds = bRect;
  502.     
  503.     myGrafPort.portRect = bRect;
  504.     
  505.     oldClipRgn = myGrafPort.clipRgn;
  506.     myGrafPort.clipRgn = (gHelp[whichHelp])->visRgn;
  507.     
  508.     SetPort(myGrafPtr);
  509.     
  510.     DrawHelp(whichHelp);
  511.     
  512.     SetPort(gHelp[whichHelp]);
  513.     
  514.     CopyBits(&(myGrafPtr->portBits),
  515.         &((gHelp[whichHelp])->portBits), &bRect, &bRect, 0, 0L);
  516.     
  517.     myGrafPort.clipRgn = oldClipRgn;
  518.     
  519.     ClosePort(myGrafPtr);
  520.     DisposPtr(myBits);
  521. }
  522.